<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/11/22
 * Time: 10:55
 */

namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;

class Recruit extends Controller
{
    protected $model = null;
    public function sign_up()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post();
            $this->check($params);
            $params['sex'] = $this->getSex($params['id_card']);
            $this->model = new \app\admin\model\Admissions();

            $means = request()->file('means');
            $meansArr = [];

            if($means)
            {
                foreach($means as $file){
                    // 移动到框架应用根目录/public/uploads/ 目录下
                    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                    if($info){
                        // 成功上传后 获取上传信息
                        // 输出 jpg
                        //echo $info->getExtension();
                        // 输出 42a79759f284b767dfcb2a0197904287.jpg
//                        echo $info->getFilename();
                        $imgs = str_replace('\\','/','/uploads/'.$info->getSaveName());
                        $meansArr[] = $imgs;
                    }else{
                        // 上传失败获取错误信息
                        $this->error($file->getError());
                    }
                }
            }

            if($meansArr)
            {
                $params['means'] = implode(',',$meansArr);
            }


            $file = request()->file('file');
            if($file==null)
            {
                $this->error('请上传缴费截图');
            }
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    // 输出 jpg
                    /* echo $info->getExtension();
                     // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                     echo $info->getSaveName();
                     // 输出 42a79759f284b767dfcb2a0197904287.jpg
                     echo $info->getFilename();*/
                    $jfjt = str_replace('\\','/','/uploads/'.$info->getSaveName());

                    $params['jfjt_images'] = $jfjt;
                }else{
                    // 上传失败获取错误信息
                    //echo $file->getError();
                    $this->error($file->getError());
                }
            }


            $result = $this->model->allowField(true)->save($params);
            if($result)
            {
                $id  = $this->model->getLastInsID();
                $this->redirect(url('recruit/welcome',['name'=>$params['name'],'id'=>$id]),302);
            }
            else
            {
                $this->error('提交失败');
            }

        }
        return $this->view->fetch();
    }

    //欢迎页面
    public function welcome($id=null,$name=null)
    {
        $name = urldecode($name);
        $this->assign('name',$name);
        $this->assign('id',$id);

        return $this->view->fetch();
    }

    public function information($id = null)
    {

        $info = Db::name('admissions')->where('id',$id)->find();
        $this->assign('info',$info);
        return $this->view->fetch();
    }

    public function countersign($type=null)
    {

        $url='';
        if($type==1)
        {
            $url = url('recruit/look_data');
        }
        else if($type==3)
        {
            $url = url('recruit/look');
        }
        else if($type==2)//补充资料
        {
            $url = url('recruit/supplement');
        }
        else
        {
            $this->error('参数错误');
        }

        $this->assign('url',$url);
        return $this->view->fetch();
    }

    //补充资料
    public function supplement()
    {
        $params = $this->request->post();

        $this->check_input($params);

        $info = Db::name('admissions')->where(['name'=>$params['name'],'id_card'=>$params['id_card']])->find();
        if(!$info)
        {
            $this->error('查无记录');
        }
        if($info['means']!='')
        {
            $info['means'] = explode(',',$info['means']);
        }
        $this->assign('info',$info);
        return $this->view->fetch();
    }

    public function supplementSub()
    {
        $params = $this->request->post();

        $model = new \app\admin\model\Admissions();
        $id = $params['id'];
        unset($params['id']);
        $means = request()->file('means');
        $meansArr = [];
        if(isset($params['old']))
        {
            foreach ($params['old'] as $k1=>$v1)
            {
                $meansArr[] = $v1;
            }
        }
        if($means)
        {
            foreach($means as $file){
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    // 输出 jpg
                    //echo $info->getExtension();
                    // 输出 42a79759f284b767dfcb2a0197904287.jpg
//                        echo $info->getFilename();
                    $imgs = str_replace('\\','/','/uploads/'.$info->getSaveName());
                    $meansArr[] = $imgs;
                }else{
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }
        }

        if($meansArr)
        {
            $params['means'] = implode(',',$meansArr);
        }
        $res = $model->allowField(true)->isUpdate(true)->save($params,['id'=>$id]);
        if($res)
        {
            $this->success('资料补充完成',url('recruit/look',['id'=>$id]));
        }
        else
        {
            $this->error('资料补充失败');
        }
        return $this->view->fetch();
    }

    public function check_input($params)
    {
        if($params['name']=='')
        {
            $this->error('姓名不能为空');
        }

        if($params['id_card']=='')
        {
            $this->error('身份证号不能为空');
        }

        $exp = "/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/";
        if(!preg_match($exp,$params['id_card'])){
            $this->error('身份证格式不正确');
        }
    }

    public function look($id)
    {
        $info = Db::name('admissions')->where('id',$id)->find();
        if(!$info)
        {
            $this->error('查无记录');
        }
        $this->assign('info',$info);
        return $this->view->fetch('recruit/information');
    }

    //查看资料
    public function look_data()
    {
        $params = $this->request->post();

        $this->check_input($params);

        $info = Db::name('admissions')->where(['name'=>$params['name'],'id_card'=>$params['id_card']])->find();
        if(!$info)
        {
            $this->error('查无记录');
        }
        $this->assign('info',$info);
        return $this->view->fetch('recruit/information');
    }
    public function getSex($idcard)
    {
        $number = substr($idcard, strlen($idcard) - 2, 1);
        if ($number % 2 == 0) { // 偶数女
            return 2;
        } else { // 基数男
            return 1;
        }
    }

    public function check($params)
    {
        if($params['name']=='')
        {
            $this->error('姓名不能为空');
        }


        if($params['mobile']=='')
        {
            $this->error('联系电话不能为空');
        }

        $exp = "/^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/";
        if(!preg_match($exp,$params['mobile'])){
            $this->error('联系电话格式不正确');
        }


        if($params['id_card']=='')
        {
            $this->error('身份证号不能为空');
        }

        $exp = "/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/";
        if(!preg_match($exp,$params['id_card'])){
            $this->error('身份证格式不正确');
        }

        $res = Db::name('admissions')->where(['name'=>$params['name'],'id_card'=>$params['id_card']])->value('id');
        if($res)
        {
            $this->error('请勿重复报名');
        }



        /*if($params['professional']=='')
        {
            $this->error('专业不能为空');
        }*/



       /* if($params['type']!=2)
        {
            if($params['standard_tuition']=='')
            {
                $this->error('请填写标准学费');
            }
        }*/

        if($params['pay_type']==0)
        {
            $this->error('请选择缴费类型');
        }
        if($params['actual_cost']=='')
        {
            $this->error('请填写实缴金额');
        }
        if($params['enrollment_teacher']=='')
        {
            $this->error('招生老师不能为空');
        }

    }
}